import { ref, shallowRef } from "vue";
import { useCountdown } from "@vueuse/core";

export interface UseCountdownOptions {
  initialCount?: number;
  onCompleted?: () => void;
}

export function useCustomCountdown(options: UseCountdownOptions = {}) {
  const { onCompleted } = options;

  const counting = ref(false);
  const countDown = shallowRef();
  const { remaining, start } = useCountdown(countDown, {
    onComplete: () => {
      counting.value = false;
      onCompleted?.();
    },
  });

  const startCountdown = (number: number) => {
    if (!counting.value) {
      counting.value = true;
      countDown.value = number;
      start();
    }
  };

  return {
    counting,
    remaining,
    startCountdown,
  };
}